<?php

class Admin_IndexController extends Zend_Controller_Action
{

    public function init()
    {
    	
    	Zend_Loader::loadClass('TeacherModel');
    	Zend_Loader::loadClass('LoginModel');
    	Zend_Loader::loadClass('StudentsModel');
    }

    function preDispatch(){
    	/*
    	 * Load template
    	 */
    	$layoutPath = APPLICATION_PATH  . '/templates/admin/admin';
    	$option = array (	'layout' 		=> 'index',
    			'layoutPath' 	=> $layoutPath );
    	Zend_Layout::startMvc ( $option );
    	/*
    	 * Tra cac thong tin ve moduln cho view
    	 */
    	$this->view->module = $this->_request->getModuleName();
    	$this->view->controller =  $this->_request->getControllerName();
    	$this->view->action =  $this->_request->getActionName();
    	/*
    	 * Kiem tra dang nhap
    	*/
    	$auth = Zend_Auth::getInstance();
    	if(strcmp($this->_request->getActionName(),'login')!=0){
    		if (!$auth->hasIdentity()) {
    			$this->_redirect('/admin/index/login');

    		}
    	}
    }
    
    function indexAction(){
    	
    }

    //----------------------------IN DON NHAP HANG---------------------------------
    public function okAction(){
    	if(1){
    	//	echo 12;die;
    		$db = Zend_Registry::get('connectDB');
    	//	$auth=new libcommon();
    		//Tao trang PDF
    		$pdfDoc = new Zend_Pdf();
    		//Tao trang zise A4
    		$pdfPage = $pdfDoc->newPage(Zend_Pdf_Page::SIZE_A4);
    		//Tao font cho trang in
    		$font = Zend_Pdf_Font::fontWithPath('font/tahoma.ttf');
    	//	$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
    		//duong dan hinh anh
    //		$imagePath = Zend_Pdf_Image::imageWithPath(APPLICATION_PATH.'/templates/admin/images/bien.jpg');
    		//$pdfPage->drawImage($imagePath, 500, 820, 600, 820);
    		
    
    		//----------------------PHAN THONG TIN CTY-----------------
    		$pdfPage->setFont($font, 13)
    		->drawText('TRUNG TÂM TRANG THIẾT BỊ TIN HỌC T&T', 20, 820, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText('Địa chỉ: Số 01,Đường 30/4,P.Xuân Khánh', 20, 800, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText('Q.Ninh Kiều, TP.Cần Thơ', 60, 780, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText('ĐT: 07103.838.925 - 0978865561', 20, 760, 'UTF-8');
    		$pdfPage->setFont($font, 18)
    		->drawText('ĐƠN NHẬP HÀNG', 230, 730, 'UTF-8');
    		//-----------PHAN THONG TIN NHA CUNG CAP & DON NHAP HANG-----------
    		$pdfPage->setFont($font, 12)
    		->drawText('- Nhà cung cấp:', 25, 700, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText('- Địa chỉ:', 25, 680, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText('- Số điện thoại:', 25, 660, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText('- Số Fax:', 25, 640, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText('- Mã số thuế:', 25, 620, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText('- Số tài khoản:', 25, 600, 'UTF-8');
    		//----------------------------------------------
    		$pdfPage->setFont($font, 12)
    		->drawText('- Mã đơn nhập hàng:', 300, 700, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText('- Ngày nhập hàng:', 300, 680, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText('- Nhân viên nhập:', 300, 660, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText('- Mã nhân viên:', 300, 640, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText('- Hình thức thanh toán:', 300, 620, 'UTF-8');
    		//-----------------------lay thong tin du lieu
    	/*	$infoDonNhapHang = $this->getDonNhapHangToPrint($_POST['data']);
    		foreach ($infoDonNhapHang as $kq):
    		$pdfPage->setFont($font, 12)
    		->drawText($kq['TEN_NHA_CC'], 106, 700, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText($kq['DCHI_NHA_CC'], 76, 680, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText($kq['SDT_NHA_CC'], 101, 660, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText($kq['FAX'], 71, 640, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText($kq['MS_THUE'], 91, 620, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText($kq['SO_TKHOAN'], 99, 600, 'UTF-8');
    		//----------------------------------------------
    		$pdfPage->setFont($font, 12)
    		->drawText($kq['STT_DNH'], 403, 700, 'UTF-8');
    		$ngay_nhap= "2012-12-01";
    		$pdfPage->setFont($font, 12)
    		->drawText($ngay_nhap, 392, 680, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText($kq['HTEN_NV'], 390, 660, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText($kq['MA_NV'], 380, 640, 'UTF-8');
    		$pdfPage->setFont($font, 12)
    		->drawText($kq['TEN_HT_TT'], 413, 620, 'UTF-8');
    		endforeach;  */
    		//-----------PHAN THONG TIN CTIET DON NHAP HANG------------
    		$pdfPage->setFont($font, 11)
    		->drawText('STT', 80, 559, 'UTF-8');
    		$pdfPage->setFont($font, 11)
    		->drawText('Tên sản phẩm', 170, 559, 'UTF-8');
    		$pdfPage->setFont($font, 11)
    		->drawText('Số Lượng', 297, 559, 'UTF-8');
    		$pdfPage->setFont($font, 11)
    		->drawText('Đơn Giá', 366, 559, 'UTF-8');
    		$pdfPage->setFont($font, 11)
    		->drawText('Thành Tiền', 441, 559, 'UTF-8');
    		//-----------------------lay thong tin du lieu
    		
    		$infoCTietNhapHang = StudentsModel::getAllStudent();
    	//	Zend_Debug::dump($infoCTietNhapHang);die;
    		$gi=0;
    		$i=1;
    		$tongTien=0;
    		
    		
    		foreach ($infoCTietNhapHang as $key => $kqCTiet) {
    			
    			$pdfPage->setFont($font, 10)
    			->drawText($i , 86, 535- $gi, 'UTF-8');
    			
    			$pdfPage->setFont($font, 10)
    			->drawText($kqCTiet['name'], 170, 535 - $gi, 'UTF-8');
    			
    			$pdfPage->setFont($font, 10)
    			->drawText($kqCTiet['shsv'], 300, 535 - $gi, 'UTF-8');
    			$pdfPage->setFont($font, 10)
    			->drawText($kqCTiet['class'], 375, 535 - $gi, 'UTF-8');
    		//	echo 12;die;
    			$pdfPage->setFont($font, 10)
    			->drawText($kqCTiet['class'], 450, 535 - $gi, 'UTF-8');
    		//	echo 12;die;
    			$gi+=25;
    			//	$tongTien+=floor(($kqCTiet['SL_NHAP'])*($kqCTiet['DGIA_NHAP']));
    			$i++;
    			
    		}
    		
    		
    		//=======================VE TABLE TRONG FILE PDF======================
    		$countRow= count($infoCTietNhapHang);
    		//-------------------LAP VE CAC COT DUNG--------------------
    		$tn=0;
    		$count=$countRow+3;
    		$diemdb_x1=65;
    		$diemdb_y1=575;
    		$diemkt_x2=515;
    		$diemkt_y2=575;
    		$pdfPage->setLineWidth(0.5);
    		$pdfPage->drawLine($diemdb_x1, $diemdb_y1, $diemkt_x2, $diemkt_y2);
    		for($i=0;$i<$count;$i++){
    			//duong ngang
    			$pdfPage->setLineWidth(0.5);
    			//------Toa do trong file: tinh tu tren xuong se giam dan
    			//------Toa do ve: diem bat dau(x1,y1) ---> diem ket thuc(x2,y2)
    			//------cap nhat lai tung do cua 2 diem bat dau va ket thuc
    			$diemdb_y1=575-$tn;
    			$diemkt_y2=575-$tn;
    			$pdfPage->drawLine($diemdb_x1, $diemdb_y1, $diemkt_x2, $diemkt_y2);
    			$tn+=25;
    		}
    		//-------------------LAP VE CAC COT DUNG--------------------
    		$td=0;
    		for($i=0;$i<6;$i++){
    			//cot dung
    			switch($i){
    				case 0:
    					$pdfPage->setLineWidth(0.5);
    					$pdfPage->drawLine(65+$td, 575, 65+$td, $diemdb_y1);
    					break;
    				case 1:
    					$pdfPage->setLineWidth(0.5);
    					$pdfPage->drawLine(85+$td, 575, 85+$td, $diemdb_y1+25);
    					break;
    				case 2:
    					$pdfPage->setLineWidth(0.5);
    					$pdfPage->drawLine(230+$td, 575, 230+$td, $diemdb_y1+25);
    					break;
    				case 3:
    					$pdfPage->setLineWidth(0.5);
    					$pdfPage->drawLine(260+$td, 575, 260+$td, $diemdb_y1+25);
    					break;
    				case 4:
    					$pdfPage->setLineWidth(0.5);
    					$pdfPage->drawLine(300+$td, 575, 300+$td, $diemdb_y1);
    					break;
    				case 5:
    					$pdfPage->setLineWidth(0.5);
    					$pdfPage->drawLine(365+$td, 575, 365+$td, $diemdb_y1);
    					break;
    			}
    			$td+=30;
    		}
    		
    		/*
    		//-----------------------tinh tong gia tri don nhap hang
    		$pdfPage->setFont($font, 12)
    		->drawText('Tổng tiền đơn nhập hàng: ', 285, $diemdb_y1+10, 'UTF-8');
    		$pdfPage->setFont($font, 10)
    		->drawText($tongTien, 435, $diemdb_y1+10, 'UTF-8');
    		
    		//-----------PHAN THONG TIN KY NHAN DON NHAP HANG------------
    		$pdfPage->setFont($font, 13)
    		->drawText('Người nhận', 90, $diemdb_y1-60, 'UTF-8');
    		$pdfPage->setFont($font, 11)
    		->drawText('(ký, ghi rõ họ tên)', 82, $diemdb_y1-80, 'UTF-8');
    		$ngay=date('d-m-Y');
    		$ngay_in=explode("-",$ngay);
    		$str = 'Cần thơ, ngày '.$ngay_in[0].' tháng '.$ngay_in[1].' năm '.$ngay_in[2];
    		$pdfPage->setFont($font, 13)
    		->drawText($str, 310, $diemdb_y1-40, 'UTF-8');
    		$pdfPage->setFont($font, 13)
    		->drawText('Nhân viên', 380, $diemdb_y1-60, 'UTF-8');
    		$pdfPage->setFont($font, 11)
    		->drawText('(ký, ghi rõ họ tên)', 370, $diemdb_y1-80, 'UTF-8');
    		//----------------------LUU FILE PDF-----------------------
    		 */
    		$pdfDoc->pages[] = $pdfPage;
    		$chars = "0123456789abcdefghijklmnoepqrstxyz";
    		$size = strlen( $chars );
    		$str = date("H_i_s_d_m_Y")."_";
    		for( $i = 0; $i < 5; $i++ ) {
    			$str .= $chars[ rand( 0, $size - 1 ) ];
    		}
    		$pdfnamefile = "file/pdf/".$str.".pdf";
    		$pdfDoc->save($pdfnamefile);
    		$this->_helper->layout->disableLayout();
    	}
    	$this->_helper->viewRenderer->setNoRender();
    	exit();
    }
    
    
    
    
    /*
     * Xử lý đăng nhập
    */
    
    public function loginAction(){
    	/*
    	 * Set layout moi cho GUI
    	*/
    	
    	$this->view->headTitle()->set('Đăng nhập');
    	$layoutPath = APPLICATION_PATH  . '/templates/admin/admin';
    	$option = array (	'layout' 		=> 'login',
    			'layoutPath' 	=> $layoutPath );
    	Zend_Layout::startMvc ( $option );
    	 
    	$request = $this->getRequest();
    	$view = $this->view;
    	/*
    	 * Khi có request từ client tiến hành nhận dữ liệu gửi lên và xử lý
    	*/
    	if ($request->isPost()) {
    		/*
    		 * Nhận dữ liệu đầu vào.
    		*/
    		$user_name = $request->getParam('username');
    		$password = $request->getParam('password');
    		$this->view->user = $user_name;
    		$this->view->pass = $password;
    		$loginModel = new LoginModel();
    		/*
    		 * Kiểm tra đăng nhập
    		*/
    		if (!$loginModel->login($user_name,$password)) {
    		}
    		else{
    			if ($this->_request->getParam('remember') == true){
    				require_once 'Zend/Session.php';
    				$time = 60 * 60 * 24 * 7 * 30;
    				Zend_Session::rememberMe($time);
    			}else {
    				Zend_Session::forgetMe();
    			}
    			$this->_redirect("/admin/index");
    		}
    	}
    }
    
    /*
     * Xử lý logout khỏi tài khoản
    */
    
    public function logoutAction(){
    	$auth = Zend_Auth::getInstance();
    	$auth->clearIdentity();
    	$this->_redirect("/admin/index/login");
    }
    
    
}